package cn.uin.domain.auth.service.user;

import cn.uin.domain.auth.adapter.repository.IAuthRepository;
import cn.uin.domain.auth.model.entity.HomeRoleEntity;
import cn.uin.domain.auth.model.entity.HomeUserEntity;
import cn.uin.domain.auth.model.entity.UserInfoDetailEntity;
import cn.uin.domain.auth.service.user.login.email.EmailCodeAuthenticationToken;
import cn.uin.domain.auth.service.user.login.phone.PhoneCodeAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName SecurityContextService
 * @Description:当前用户信息
 * @Author: Administrator
 * @CreateDate: 2025/11/5 15:46
 * @Version: 1.0
 */
@Component
public class SecurityContextService {

	@Resource
	private IAuthRepository iAuthRepository;

	/**
	 * 获取当前用户信息
	 * @return
	 */
	public HomeUserEntity getCurrentUser() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		String principal = (String) authentication.getPrincipal();
		HomeUserEntity homeUserEntity = null;
		if (authentication instanceof UsernamePasswordAuthenticationToken) {
			// 根据用户名查询用户
			homeUserEntity = iAuthRepository.queryUserByUserName(principal);
		} else if (authentication instanceof EmailCodeAuthenticationToken) {
			// 根据邮箱查询用户
			homeUserEntity = iAuthRepository.queryUserByUserEmail(principal);
		} else if (authentication instanceof PhoneCodeAuthenticationToken) {
			// 根据手机号查询用户
			homeUserEntity = iAuthRepository.queryUserByUserPhone(principal);
		}
		return homeUserEntity;
	}

}
